package com.comdo.newup_loan.module.limit;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.model.CarByCodeBean;
import com.comdo.newup_loan.model.CarCityBean;
import com.comdo.newup_loan.model.CarCityItemBean;
import com.comdo.newup_loan.model.CarConfigsBean;
import com.comdo.newup_loan.util.StringUtils;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.dateSelect.DatePicker;
import com.comdo.newup_loan.widget.mySpinner.ComomData;
import com.comdo.newup_loan.widget.mySpinner.MySpinner;
import com.comdo.newup_loan.widget.mySpinner.onResultListener;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/2.
 */

public class CarLimitActivity extends BaseActivity<CarLimitPersenter> implements CarLimitContact.View, View.OnClickListener {
    @BindView(R.id.ed_car_no)
    EditText mCarNoEditText;
    @BindView(R.id.tv_car_date)
    TextView mCarDateTextView;
    @BindView(R.id.tv_car_color)
    TextView mCarColorTextView;
    @BindView(R.id.tv_province)
    TextView mProvinceTextView;
    @BindView(R.id.tv_city)
    TextView mCityTextView;
    @BindView(R.id.ed_distance)
    EditText mDistanceEditText;
    @BindView(R.id.ed_car_config)
    TextView mconfigTextView;
    @BindView(R.id.btn_select_brand)
    Button mSelectBrandBtn;
    @BindView(R.id.btn_submit)
    Button mSubmitBtn;
    @BindView(R.id.tv_amount)
    TextView amountTextView;
    //颜色，省，配置
    private static CarConfigsBean datas;
    private DatePicker dataPicker;
    private MySpinner sp_province, sp_color, sp_city, sp_config;
    private String[] colors = {};
    private String[] configs = {};
    private List<CarCityItemBean> mCitys = new ArrayList<CarCityItemBean>();
    private List<CarCityItemBean> provinces = new ArrayList<CarCityItemBean>();
    private int provinceId, cityId;
    private String carModelName;
    private int carModelId;
    private String regData;
    private Double mile;
    private String color;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_car_limit;
    }

    @Override
    protected void init() {
        super.init();
        if (datas == null)
            mPresenter.getConfigtList();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(CarLimitActivity.this, "车辆借款额度查询", true);
        mCarDateTextView.setOnClickListener(this);
        mCarColorTextView.setOnClickListener(this);
        mProvinceTextView.setOnClickListener(this);
        mCityTextView.setOnClickListener(this);
        mconfigTextView.setOnClickListener(this);
        mSelectBrandBtn.setOnClickListener(this);
        mSubmitBtn.setOnClickListener(this);
        mCarNoEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //车架号为17位
                if (s.length() == 17) {
                    mPresenter.getByCarCode(s.toString());
                }
                if (s.length() == 0) {
                    mSelectBrandBtn.setEnabled(true);
                    mSelectBrandBtn.setText("点击选择品牌");
                }
            }
        });
    }


    @Override
    public CarLimitPersenter getmPresenter() {
        return new CarLimitPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getConfigFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void getConfigtList(CarConfigsBean datas) {
        colors = datas.getColor();
        configs = datas.getConfiguration();
        mCitys.clear();
        for (CarCityBean item : datas.getProvince()) {
            if (item.getList() != null & item.getList().size() > 0) {
                provinces.addAll(item.getList());
            }
        }
    }

    @Override
    public void getCityList(List<CarCityItemBean> citys) {
        this.mCitys.clear();
        this.mCitys.addAll(citys);
        if (citys.size() == 0) {
            showToast("没有数据！");
            return;
        }

            sp_city = new MySpinner(CarLimitActivity.this, mCitys, 1);
            sp_city.setOnResultListener(new onResultListener() {
                @Override
                public void onResult(Object obj) {
                    if (obj != null) {
                        mCityTextView.setText(((ComomData) obj)
                                .getMyName());
                        cityId = ((ComomData) obj)
                                .getMyId();
                    }
                }
            });

    }

    @Override
    public void assess(Double amount) {
        amountTextView.setText(amount + "万元");
    }

    @Override
    public void getByCarCode(CarByCodeBean carByCodeBean) {
        carModelId = carByCodeBean.getModel_id();
        mSelectBrandBtn.setEnabled(false);
        mSelectBrandBtn.setText(carByCodeBean.getCarBaseInfo());
    }

    private void toBrandActivity() {
        Intent intent = new Intent(CarLimitActivity.this, CarBrandActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.tv_car_date:
                if (dataPicker == null) {
                    dataPicker = new DatePicker(CarLimitActivity.this);
                    dataPicker.selectDateDialog((TextView) v, "1990-01-01", 0);
                    dataPicker.show();
                } else {
                    dataPicker.show();
                }
                break;
            case R.id.tv_car_color:
                if (colors.length == 0) {
                    showToast("没有数据！");
                    return;
                }
                if (sp_color == null) {
                    sp_color = new MySpinner(CarLimitActivity.this, colors, 1, "");
                    sp_color.setOnResultListener(new onResultListener() {
                        @Override
                        public void onResult(Object obj) {
                            if (obj != null) {
                                ((TextView) v).setText(((ComomData) obj)
                                        .getMyName());
                            }
                        }
                    });
                } else {
                    sp_color.show();
                }
                break;
            case R.id.tv_province:
                if (provinces.size() == 0) {
                    showToast("没有数据！");
                    return;
                }

                if (sp_province == null) {
                    sp_province = new MySpinner(CarLimitActivity.this, provinces, 1);
                    sp_province.setOnResultListener(new onResultListener() {
                        @Override
                        public void onResult(Object obj) {
                            if (obj != null) {
                                ((TextView) v).setText(((ComomData) obj)
                                        .getMyName());
                                provinceId = ((ComomData) obj)
                                        .getMyId();
                            }
                        }
                    });
                } else {
                    sp_province.show();
                }
                break;
            case R.id.tv_city:
                if (provinceId == 0) {
                    showToast("请选择省份！");
                } else {
                    mPresenter.getCityList(provinceId);
                }

                break;
            case R.id.ed_car_config:
                if (configs.length == 0) {
                    showToast("没有数据！");
                    return;
                }
                if (sp_config == null) {
                    sp_config = new MySpinner(CarLimitActivity.this, configs, 1, "");
                    sp_config.setOnResultListener(new onResultListener() {
                        @Override
                        public void onResult(Object obj) {
                            if (obj != null) {
                                ((TextView) v).setText(((ComomData) obj)
                                        .getMyName());
                            }
                        }
                    });
                } else {
                    sp_config.show();
                }
                break;
            case R.id.btn_select_brand:
                toBrandActivity();
                break;
            case R.id.btn_submit:
                regData= mCarDateTextView.getText().toString();
                if(!StringUtils.isNull(mDistanceEditText.getText().toString()))
                mile = Double.parseDouble(mDistanceEditText.getText().toString());
                color = mCarColorTextView.getText().toString();
                if(validate())
                mPresenter.assess(carModelId,regData ,mile , cityId, color);
                break;
        }
    }

    private boolean validate() {
        if(carModelId==0){
            showToast("请选择品牌或输入车架号!");
            return false;
        }
        if(StringUtils.isNull(regData)){
            showToast("请选择上牌年份!");
            return false;
        }
        if(StringUtils.isNull(color)){
            showToast("请选择车辆颜色!");
            return false;
        }
        if(carModelId==0){
            showToast("请选城市!");
            return false;
        }
        if(StringUtils.isNull(mDistanceEditText.getText().toString())){
            showToast("请输入行驶里程数!");
            return false;
        }
        if(StringUtils.isNull(mconfigTextView.getText().toString())){
            showToast("请选择配置!");
            return false;
        }
        return true;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CarBrandActivity.RESULT_CODE) {
            carModelName = data.getStringExtra("name");
            carModelId = data.getIntExtra("id", 0);
            if (!StringUtils.isNull(carModelName))
                mSelectBrandBtn.setText(carModelName);
        }
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("页面车辆额度查询");
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("页面车辆额度查询");
        MobclickAgent.onPause(this);
    }
}

